#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Dict, Type, List, Optional
from .base import FilterRule

class FilterRegistry:
    """统一的过滤器注册表 - 提供注册、创建和配置管理功能"""
    
    # 过滤器注册表
    _registry: Dict[str, Type[FilterRule]] = {}
    
    @classmethod
    def register(cls, name: str, filter_class: Type[FilterRule]):
        """注册过滤器类"""
        cls._registry[name] = filter_class
    
    @classmethod
    def get(cls, name: str) -> Type[FilterRule]:
        """获取过滤器类"""
        if name not in cls._registry:
            raise ValueError(f"未找到过滤器: {name}")
        return cls._registry[name]
    
    @classmethod
    def create(cls, name: str, **kwargs) -> FilterRule:
        """创建过滤器实例"""
        filter_class = cls.get(name)
        return filter_class(**kwargs)
    
    @classmethod
    def create_filters_from_mapping(cls, config_mapping: Dict[str, Dict]) -> List[FilterRule]:
        """从 映射{规则名: 参数} 创建过滤器列表（按注册顺序不保证稳定）"""
        filters = []
        for name, params in config_mapping.items():
            params = params or {}
            filter_instance = cls.create(name, **params)
            filters.append(filter_instance)
        return filters
    
    @classmethod
    def list_available(cls) -> List[str]:
        """列出所有可用的过滤器名称"""
        return list(cls._registry.keys())
    
    @classmethod
    def is_registered(cls, name: str) -> bool:
        """检查过滤器是否已注册"""
        return name in cls._registry

# 统一的过滤器注册函数
def register_all_filters():
    """注册所有内置过滤器"""
    try:
        from .type_base_filter import TypeBaseFilter
        from .trivial_whitespace_filter import TrivialWhitespaceFilter
        from .code_block_language_tag_filter import CodeBlockLanguageTagFilter
        from .url_change_filter import UrlChangeFilter
        from .file_extension_filter import FileExtensionFilter
        from .markdown_heading_level_filter import MarkdownHeadingLevelFilter
        from .punctuation_change_filter import PunctuationChangeFilter
        from .list_style_change_filter import ListStyleChangeFilter
        from .content_length_filter import ContentLengthFilter
        from .trailing_symbol_change_filter import TrailingSymbolChangeFilter
        
        FilterRegistry.register('type_base', TypeBaseFilter)
        FilterRegistry.register('trivial_whitespace', TrivialWhitespaceFilter)
        FilterRegistry.register('code_block_language_tag', CodeBlockLanguageTagFilter)
        FilterRegistry.register('url_change', UrlChangeFilter)
        FilterRegistry.register('file_extension', FileExtensionFilter)
        FilterRegistry.register('markdown_heading_level', MarkdownHeadingLevelFilter)
        FilterRegistry.register('punctuation_change', PunctuationChangeFilter)
        FilterRegistry.register('list_style_change', ListStyleChangeFilter)
        FilterRegistry.register('content_length', ContentLengthFilter)
        FilterRegistry.register('trailing_symbol_change', TrailingSymbolChangeFilter)
        
    except ImportError as e:
        raise e

# 初始化时自动注册所有过滤器
register_all_filters() 